from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django.shortcuts import render,redirect
from django.contrib.auth.hashers import make_password, check_password
from django.urls import reverse
from .. import models
import time,os
from django.db.models import Q
from shopmall.settings import BASE_DIR

from django.core.paginator import Paginator

# 商品种类列表
def category(request):
    # return render(requset,'myadmin/category/category.html')
    cates = tab()

    types = request.GET.get('type')
    # 接受关键字
    search = request.GET.get('search')
    # 判断用是否搜索内容
    if types:
        if types == 'all':
            cates = models.Category.objects.filter(cname__contains=search)
            cates = tab11(cates)
        elif types == 'cname':
            cates = models.Category.objects.filter(cname__contains=search)
            cates = tab11(cates)
    # 实例化分页对象
    p = Paginator(cates, 15)

    #一共可以分多少页
    sumpage=p.num_pages

    # 取第几页的数据
     # 接受用户的页码
    page = int(request.GET.get('p',1))
    # 第几页的数据
    page1 = p.page(page)

    # 判断 如果用输入的页码<=3 显示前五个页码
    if page<=3:
        # 页码的迭代序列  [1,2,3,4,5,6,7]
        prange = p.page_range[:5]
    elif page+2>=sumpage:
        prange = p.page_range[-5:]
    else:
        prange = p.page_range[page-3:page+2]

    # 进行数据分配 通过 模板语法将数据展示到模板当中
    return render(request,'myadmin/category/category.html',{'cates':page1,'prange':prange,'page':page,'sumpage':sumpage})


# 添加商品种类
def addcategory(request):
    # return render(request,'myadmin/category/addcategory.html')
    if request.method=='GET':
        
        cates = tab()
        return render(request,'myadmin/category/addcategory.html',{'cates':cates})
    elif request.method=='POST':
        # 接受pid 通过pid来判断是不是顶级分类
        pid = request.POST.get('pid')
        name = request.POST.get('name')
        print(pid)
        print(name)
        if pid=='0':
            cate = models.Category()
            cate.cname=name
            cate.cid=int(pid)
            cate.cpath='0,'
            cate.save()
        else:
            # 根据pid找父级 paths
            pobj = models.Category.objects.get(id=pid)
            c = models.Category()
            c.cname=name
            c.cid=pobj.id
            c.cpath=pobj.cpath+pid+','
            c.save()
        # 接受数据添加数据
        return HttpResponse("<script>alert('添加成功!');location.href='" + reverse('myadmin_addcategory') + "'</script>")

def tab():
    cates = models.Category.objects.extra(select = {'newpath':'concat(cpath,id)'}).order_by('newpath')
    for i in cates:
        num = i.cpath.count(',')-1
        i.newname=num*'|----'
    return cates

def tab11(cates):
    for i in cates:
        num = i.cpath.count(',')-1
        i.newname=num*'|----'
    return cates


def delcate(request):
    # 接受id 删除数据的id
    pid = int(request.GET.get('pid'))
    # 判断有没有子分类
    cnum = models.Category.objects.filter(cid=pid).count()
    print(cnum)
    if cnum:
        return JsonResponse({'msg':0})
    else:
        c = models.Category.objects.get(id=pid)
        c.delete()
        return JsonResponse({'msg':1})

# 修改
def editcate(request):
    # 接受id和name
    ccid = int(request.GET.get('id'))
    ccname = request.GET.get('name')
    try:

        cate = models.Category.objects.get(id=ccid)
        print('cate',cate)
        cate.cname=ccname
        cate.save()
        return JsonResponse({'msg':1})
    except :
        return JsonResponse({'msg':0})
